package playnsimple.core.state;
import playnsimple.core.PlaynSimple;

public abstract class BaseState {
    
    // required fields
    protected String label;
    protected StateMachine machine;

    // timing fields
    protected long resumeAt = 0;
    protected long timeIn;
    
    /*
     * State timer: simple timer to delay/schedule events
     * Time is marked in milliseconds
     */
    public static class StateTimer {
        
        private long startedAt;
        private long expiresAt;
        
        public static StateTimer start(Number lifespan) {
            StateTimer stateTimer = new StateTimer(lifespan.longValue());
            return stateTimer;
        }
        
        public StateTimer(long lifespan) {
            this.startedAt = now();
            this.expiresAt = this.startedAt + lifespan;
        }
        
        private long now() {
            return System.currentTimeMillis();
        }   
        
        public boolean isExpired() {
            return now() >= expiresAt;
        }
    }

    /*
     * Core Methods
     */
    public BaseState() {
        throw new IllegalStateException(
                "StateController constructor requires a label argument");
    }
    
    public BaseState(String label) {
        this.label = label;
    }

    abstract public void update();
    
    public void paint() {
        
    }

    public void enter() {
        timeIn = System.currentTimeMillis();
        PlaynSimple.log("entering state <" + label + ">");
    }

    public void exit() {
        PlaynSimple.log("exiting state <" + label + ">");
    }

    /*
     * Getters / Setters
     */
    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }
}
